package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.TextAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.Place;
import com.nantai.jiafang.nantaijiafang.deviceManager.Constans;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.Error;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.HttpClient;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.ResponseHandler;
import com.nantai.jiafang.nantaijiafang.deviceManager.preference.PreferencesUtil;
import com.nantai.jiafang.nantaijiafang.deviceManager.utils.MessagePop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class PutInActivity extends BaseActivity implements View.OnClickListener{

    //@InjectView(R.id.code_tx)
    EditText mCodeTx;
    //@InjectView(R.id.scan_btn)
    Button mScanBtn;
   // @InjectView(R.id.type_spinner)
    Spinner mTypeSpinner;
    //@InjectView(R.id.place_spinner)
    Spinner mPlaceSpinner;
    //@InjectView(R.id.action_btn)
    Button mActionBtn;
    //@InjectView(R.id.tv_toolbar_title)
    TextView mTvToolbarTitle;
    //@InjectView(R.id.toolbar)
    Toolbar mToolbar;


    private List<Place> place_list;
    private Place select_place=null;
    private String operate_type=null;
    private MaterialDialog showDialog=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_put_in);
        mToolbar=(Toolbar)findViewById(R.id.toolbar);
        mTvToolbarTitle =(TextView)findViewById(R.id.tv_toolbar_title);
        mActionBtn=(Button)findViewById(R.id.action_btn);
        mActionBtn.setOnClickListener(this);
        mPlaceSpinner=(Spinner)findViewById(R.id.place_spinner);
        mTypeSpinner=(Spinner)findViewById(R.id.type_spinner);
        mScanBtn=(Button)findViewById(R.id.scan_btn);
        mScanBtn.setOnClickListener(this);
        mCodeTx=(EditText)findViewById(R.id.code_tx);
        place_list= PreferencesUtil.getPlaceList(PutInActivity.this);
        if(place_list==null)
        {
            place_list =new ArrayList();
        }
        mTvToolbarTitle.setText("入库");
        mToolbar.setNavigationIcon(R.drawable.md_nav_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        List mItems= Arrays.asList("采购入库", "维修入库", "调拨入库");
        SpinnerAdapter _adapter = new TextAdapter(this,mItems);
        mTypeSpinner.setAdapter(_adapter);
        mTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                operate_type = String.valueOf(pos+1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mTypeSpinner.setSelection(0);
        operate_type="1";
        List place_names= new ArrayList();
        for(Place place :place_list)
        {
            place_names.add(place.getName());
        }

        SpinnerAdapter _placeadapter = new TextAdapter(this,place_names);
        mPlaceSpinner.setAdapter(_placeadapter);
        mPlaceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()  {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id){
                select_place= place_list.get(pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mTypeSpinner.setSelection(0);
        showDialog = new MaterialDialog.Builder(PutInActivity.this)
                .content("正在请求入库")
                .progress(true, 0, false)
                .cancelable(false).build();
    }

    @Override//({R.id.scan_btn, R.id.action_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scan_btn: {
                Intent intent = new Intent(PutInActivity.this, CaptureActivity.class);
                intent.putExtra("operate_type", String.valueOf(Constans.ScanCode));
                startActivityForResult(intent, 20);
            }
            break;
            case R.id.action_btn:
            {
                if(mCodeTx.getText()==null||mCodeTx.getText().toString().equals(""))
                {
                    MessagePop.ToastMessage(PutInActivity.this, "请输入设备条码");
                    return;
                }
                if(select_place==null)
                {
                    MessagePop.ToastMessage(PutInActivity.this, "请选择库位");
                    return;
                }
                if(operate_type==null)
                {
                    MessagePop.ToastMessage(PutInActivity.this, "请选择入库方式");
                    return;
                }
                showDialog.show();
                Map<String, String> params = new HashMap<>();
                String qrcode = mCodeTx.getText().toString();
                params.put("qrcode",qrcode);
                params.put("operate_type",operate_type);
                params.put("place_id",select_place.getId());
                params.put("place_name",select_place.getName());
                HttpClient client = new HttpClient(PutInActivity.this);
                Call call = client.service().putIn(params);
                client.request(call, new ResponseHandler<ResponseBody>() {
                    @Override
                    public void onSuccess(ResponseBody res) {
                        //登陆成功，保存用户信息
                        showDialog.dismiss();
                        MessagePop.ToastMessage(PutInActivity.this, "入库成功");
                    }
                    @Override
                    public void onFailure(int code, Error e) {
                        showDialog.dismiss();
                        if(code!=404)
                        {
                            MessagePop.ToastMessage(PutInActivity.this,e.getMessage());
                        }else
                        {
                            MessagePop.ToastMessage(PutInActivity.this, "网络异常");
                        }

                    }
                });

            }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //可以根据多个请求代码来作相应的操作
        if (20 == resultCode) {
            String qrcode =data.getStringExtra("qrcode");
            mCodeTx.setText(qrcode==null?"":qrcode);
        }
    }
}
